#include "displaywindow.h"
#include "ui_displaywindow.h"
#include "ffplay_stater.h"
#include <QDebug>
//int displaywindow::Draw(const Frame *frame)
//{
////    QMutexLocker locker(&m_mutex);

//    QMutexLocker locker(&m_mutex);
//    if (!frame || !frame->frame->data[0]) {
//        qDebug() << "Invalid frame or empty frame data";
//        return -1;  // 直接返回，避免后续操作导致错误
//    }

//    if (frame->frame->width <= 0 || frame->frame->height <= 0) {
//        qDebug() << "Invalid frame dimensions: width" << frame->frame->width << ", height" << frame->frame->height;
//        return -1;
//    }
//    qDebug() << "Frame dimensions: width =" << frame->frame->width << ", height =" << frame->frame->height;
//    qDebug() << "Frame data[0] address:" << (void*)frame->frame->data[0];
//    qDebug() << "Frame linesize[0]:" << frame->frame->linesize[0];
////    if(0){
////        QImage imageTmp = QImage((uint8_t *)frame->frame->data[0],
////                                 frame->width, frame->height,
////                                 QImage::Format_RGB888);  // 确保像素格式匹配
////        img = imageTmp.copy(0,0,frame->width,frame->height);
////        update();
////        return 1;
////    }
//        if(!img_scaler_) {
//            int win_width = width();
//            int win_height = height();
//            video_width = frame->width;
//            video_height = frame->height;
//            img_scaler_ = new picscale();
//            double video_aspect_ratio = frame->width *1.0 / frame->height;
//            double win_aspect_ratio = win_width*1.0 / win_height;
//            if(win_aspect_ratio > video_aspect_ratio) {
//                //此时应该是调整x的起始位置，以高度为基准
//                img_height = win_height;
//                if(img_height %2 != 0) {
//                    img_height -= 1;
//                }

//                img_width = img_height*video_aspect_ratio;
//                if(img_width %2 != 0) {
//                    img_width -= 1;
//                }
//                y_ = 0;
//                x_ = (win_width - img_width) / 2;
//            } else {
//                //此时应该是调整y的起始位置，以宽度为基准
//                img_width = win_width;
//                if(img_width %2 != 0) {
//                    img_width -= 1;
//                }
//                img_height = img_width / video_aspect_ratio;
//                if(img_height %2 != 0) {
//                    img_height -= 1;
//                }
//                x_ = 0;
//                y_ = (win_height - img_height) / 2;
//            }
//            img_scaler_->init(video_width, video_height, frame->format,
//                              img_width, img_height, AV_PIX_FMT_RGB24);
//            memset(&dst_video_frame_, 0, sizeof(VideoFrame));
//            dst_video_frame_.width = img_width;
//            dst_video_frame_.height = img_height;
//            dst_video_frame_.format = AV_PIX_FMT_RGB24;
//            dst_video_frame_.data[0] = (uint8_t*)malloc(img_width * img_height * 3);
//            dst_video_frame_.linesize[0] = img_width * 3; // 每行的字节数
//        }
//        img_scaler_->sale(frame, &dst_video_frame_);

//        QImage imageTmp =  QImage((uint8_t *)dst_video_frame_.data[0],
//                img_width, img_height, QImage::Format_RGB888);
//        img = imageTmp.copy(0, 0, img_width, img_height);

//        update();
//    //    repaint();
//        return 0;
//}

int displaywindow::Draw(const Frame *frame)
{
    QMutexLocker locker(&m_mutex);

    if(!img_scaler_) {
        int win_width = width();
        int win_height = height();
        video_width = frame->width;
        video_height = frame->height;
        img_scaler_ = new picscale();
        double video_aspect_ratio = frame->width *1.0 / frame->height;
        double win_aspect_ratio = win_width*1.0 / win_height;
        if(win_aspect_ratio > video_aspect_ratio) {
            //此时应该是调整x的起始位置，以高度为基准
            img_height = win_height;
            if(img_height %2 != 0) {
                img_height -= 1;
            }

            img_width = img_height*video_aspect_ratio;
            if(img_width %2 != 0) {
                img_width -= 1;
            }
            y_ = 0;
            x_ = (win_width - img_width) / 2;
        } else {
            //此时应该是调整y的起始位置，以宽度为基准
            img_width = win_width;
            if(img_width %2 != 0) {
                img_width -= 1;
            }
            img_height = img_width / video_aspect_ratio;
            if(img_height %2 != 0) {
                img_height -= 1;
            }
            x_ = 0;
            y_ = (win_height - img_height) / 2;
        }
        img_scaler_->init(video_height, video_width, frame->format,
                         img_height ,img_width , AV_PIX_FMT_RGB24);
        memset(&dst_video_frame_, 0, sizeof(VideoFrame));
        dst_video_frame_.width = img_width;
        dst_video_frame_.height = img_height;
        dst_video_frame_.format = AV_PIX_FMT_RGB24;
        dst_video_frame_.data[0] = (uint8_t*)malloc(img_width * img_height * 3);
        dst_video_frame_.linesize[0] = img_width * 3; // 每行的字节数
    }
  int  ret =  img_scaler_->sale(frame, &dst_video_frame_);
  if(ret<0){
    std::cout<<"scale error"<<std::endl;
  }
    QImage imageTmp =  QImage((uint8_t *)dst_video_frame_.data[0],
            img_width, img_height, QImage::Format_RGB888);
    img = imageTmp.copy(0, 0, img_width, img_height);

    update();
//    repaint();
    return 0;
}

//int displaywindow::Draw(const Frame *frame)
//{
//    QMutexLocker locker(&m_mutex);

//    if(!img_scaler_) {
//        int win_width = width();
//        int win_height = height();
//        video_width = frame->width;
//        video_height = frame->height;
//        img_scaler_ = new ImageScaler();
//        double video_aspect_ratio = frame->width *1.0 / frame->height;
//        double win_aspect_ratio = win_width*1.0 / win_height;
//        if(win_aspect_ratio > video_aspect_ratio) {
//            //此时应该是调整x的起始位置，以高度为基准
//            img_height = win_height;
//            if(img_height %2 != 0) {
//                img_height -= 1;
//            }

//            img_width = img_height*video_aspect_ratio;
//            if(img_width %2 != 0) {
//                img_width -= 1;
//            }
//            y_ = 0;
//            x_ = (win_width - img_width) / 2;
//        } else {
//            //此时应该是调整y的起始位置，以宽度为基准
//            img_width = win_width;
//            if(img_width %2 != 0) {
//                img_width -= 1;
//            }
//            img_height = img_width / video_aspect_ratio;
//            if(img_height %2 != 0) {
//                img_height -= 1;
//            }
//            x_ = 0;
//            y_ = (win_height - img_height) / 2;
//        }
//        img_scaler_->Init(video_width, video_height, frame->format,
//                          img_width, img_height, AV_PIX_FMT_RGB24);
//        memset(&dst_video_frame_, 0, sizeof(VideoFrame));
//        dst_video_frame_.width = img_width;
//        dst_video_frame_.height = img_height;
//        dst_video_frame_.format = AV_PIX_FMT_RGB24;
//        dst_video_frame_.data[0] = (uint8_t*)malloc(img_width * img_height * 3);
//        dst_video_frame_.linesize[0] = img_width * 3; // 每行的字节数
//    }
//    img_scaler_->Scale3(frame, &dst_video_frame_);

//    QImage imageTmp =  QImage((uint8_t *)dst_video_frame_.data[0],
//            img_width, img_height, QImage::Format_RGB888);
//    img = imageTmp.copy(0, 0, img_width, img_height);

//    update();
////    repaint();
//    return 0;
//}


void displaywindow::DeInit()
{
    if(dst_video_frame_.data[0]) {
        free(dst_video_frame_.data[0]);
        dst_video_frame_.data[0] = NULL;
    }
//    if(img_scaler_) {
//        delete img_scaler_;
//        img_scaler_ = NULL;
//    }
}

void displaywindow::StartPlay()
{
    QMutexLocker locker(&m_mutex);
    play_state_ = 1;
    return ;

}

void displaywindow::StopPlay()
{
    QMutexLocker locker(&m_mutex);
    play_state_ = 2;
        update();
}

displaywindow::displaywindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::displaywindow)
{
    ui->setupUi(this);
//    win_width_ = width();
//    win_height_ = height();
    memset(&dst_video_frame_, sizeof(VideoFrame), 0);
//    play_state_ = 2;
//    ui->setupUi(this);
}

displaywindow::~displaywindow()
{
    DeInit();
    delete ui;
}

void displaywindow::paintEvent(QPaintEvent *)
{
    QMutexLocker locker(&m_mutex);
    if (img.isNull()) {
        return;
    }
    QPainter painter(this);

    //    //    p.translate(X, Y);
    //    //    p.drawImage(QRect(0, 0, W, H), img);
    QRect rect = QRect(x_, y_, img.width(), img.height());
    painter.drawImage(rect, img);
}

void displaywindow::resizeEvent(QResizeEvent *event)
{
    QMutexLocker locker(&m_mutex);
    if(win_width_ != width() || win_height_ != height()) {
        //        DeInit();       // 释放尺寸缩放资源，等下一次draw的时候重新初始化
        //        win_width = width();
        //        win_height = height();
        req_resize_ = true;
    }
}


//void displaywindow::paintEvent(QPaintEvent *)
//{
//    QMutexLocker locker(&m_mutex);
//    if(play_state_ == 1) {  // 播放状态
//        if (img.isNull()) {
//            return;
//        }
//        QPainter painter(this);
//        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
//        //    //    p.translate(X, Y);
//        //    //    p.drawImage(QRect(0, 0, W, H), img);
//        QRect rect = QRect(x_, y_, img.width(), img.height());
//        //        qDebug() << rect << ", win_w:" << this->width() << ", h:" << this->height();
//        painter.drawImage(rect, img.scaled(img.width(), img.height()));
//    } else if(play_state_ == 2) {
//        QPainter p(this);
//        p.setPen(Qt::NoPen);
//        p.setBrush(Qt::black);
//        p.drawRect(rect());
//    }
////}
